<template>
    <div class="import-container">
        <el-form ref="form" label-width="130px">
            <el-form-item>
                <el-button type="primary" @click="importData">导出</el-button>
                <el-button @click="chooseAll">全选</el-button>
                <el-button @click="inverse">反选</el-button>
                <el-button>取消</el-button>
            </el-form-item>
            <el-row>
                <el-col
                    v-for="item in formField"
                    :key="item.fieldId"
                    :xs="24"
                    :sm="12"
                    :md="8"
                    :lg="8"
                    :xl="6">
                    <el-form-item :label="item.fieldLableName">
                        <el-switch
                            v-model="item.isAuth"
                            active-color="#13ce66"
                            inactive-color="#ff4949"
                            active-value="Y"
                            inactive-value="N"/>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
    </div>
</template>

<script>
import { getFormField, exportCardformInitData } from '@/api/api'
import Cookies from 'js-cookie'
const session = JSON.parse(Cookies.get('sessionData'))
export default {
    name: 'exportData',
    props: {
        exportExcelData: Object
    },
    data() {
        return {
            formField: []
        }
    },
    created() {
        this.init()
    },
    methods: {
        init() {
            getFormField(this.exportExcelData.formId)
                .then(res => {
                    console.log(res)
                    this.formField = res
                    for (var j = 0; j < this.formField.length; j++) {
                        if (this.formField[j] === '' || this.formField[j] === null || typeof (this.formField[j]) === undefined) {
                            this.formField.splice(j, 1)
                            j = j - 1
                        }
                    }
                })
        },
        importData() {
            const fieldJson = {}
            for (let i = 0; i < this.formField.length; i++) {
                const fieldCode = this.formField[i].fieldTalbename
                const fieldCodeCheck = fieldCode + '_check'
                fieldJson[fieldCode] = this.formField[i].isAuth
            }
            this.exportData(fieldJson)
        },
        exportData(fieldArray) {
            var _filterdata = {
                buttonId: this.exportExcelData.buttonId,
                containerParam: {},
                fieldArray,
                filename: this.exportExcelData.filename,
                formId: this.exportExcelData.formId,
                filter: this.exportExcelData.filterData,
                userIds: session.userid,
                isView: null,
                heightGrade: [],
                paramsMap: {},
                requestParam: this.exportExcelData.requestParam,
                sortParams: {}

            }
            exportCardformInitData(_filterdata)
                .then(text => {
                    console.log(text)
                    if (text[0] === 0) {
                        alert('导出失败！')
                    } else if (text[0] === 'error') {
                        alert('操作异常')
                    } else if (text[0] === 'BIG') {
                        window.open(`/myehr/form/getExportBigFile.action?buttonId=${this.exportExcelData.buttonId}`)
                    } else if (text[0] === 'SMALL') {
                        window.open(`/myehr/form/getExportFile.action?buttonId=${this.exportExcelData.buttonId}`)
                    }
                })
        },
        chooseAll() {
            this.formField.forEach(item => {
                item.isAuth = 'Y'
            })
        },
        inverse() {
            this.formField.forEach(item => {
                item.isAuth = item.isAuth === 'Y' ? 'N' : 'Y'
            })
        }
    }
}
</script>

